﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Entities;
using System.Data.Objects;
namespace Data
{
    public class PhieuNhapRepository
    {
        public static List<PhieuNhapDTO> GetPhieuNhapList()
        {
            NhaSachEntities context = new NhaSachEntities();
            var q = from pn in context.PhieuNhapList
                    select new PhieuNhapDTO
                    {
                        MaPhieuNhap = pn.MaPhieuNhap,
                        NgayNhap = (DateTime)pn.NgayNhap,
                        TinhTrang=(int)pn.TinhTrang
                    };
            return q.ToList();

        }
        public static List<SachDTO> GetPhieuNhapDetail(int maPhieuNhap)
        {
            NhaSachEntities context = new NhaSachEntities();
            //var q = from ct in context.ChiTietPhieuNhapList
            //        join s in context.SachList on ct.Sach.MaSach equals s.MaSach
            //        where ct.PhieuNhap.MaPhieuNhap == MaPhieuNhap
            //        select new SachDTO
            //        {
            //            MaSach = s.MaSach,
            //            TenSach = s.TenSach,
            //            TheLoai = s.TheLoai.MaTheLoai,
            //            MaTacGia = s.TacGia.MaTacGia,
            //            SoLuong = (int)s.SoLuong,
            //            DonGia = (double)s.DonGia,
            //            TinhTrang = (int)s.TinhTrang
            //        };
            var q = from ct in context.ChiTietPhieuNhapList
                     where ct.PhieuNhap.MaPhieuNhap == maPhieuNhap
                     select new SachDTO(){
                        MaSach = ct.Sach.MaSach,
                        TenSach = ct.Sach.TenSach,
                        MaTheLoai = ct.Sach.TheLoai.MaTheLoai,
                        MaTacGia = ct.Sach.TacGia.MaTacGia,
                        SoLuong = (int)ct.Sach.SoLuong,
                        DonGia = (double)ct.Sach.DonGia,
                        TinhTrang = (int)ct.Sach.TinhTrang
                     };
             
            
            return q.ToList();
        }
        public static void InsertPhieuNhap(List<ChiTietPhieuNhapDTO> list,DateTime ngayNhap)
        {
            NhaSachEntities context = new NhaSachEntities();
            PhieuNhap pn = new PhieuNhap();
            pn.NgayNhap = ngayNhap;
            pn.TinhTrang = 1;
            foreach (ChiTietPhieuNhapDTO i in list)
            {
                ChiTietPhieuNhap ct = new ChiTietPhieuNhap();
                var q = (from s in context.SachList
                         where s.MaSach == i.MaSach
                         select s).First();
                ct.Sach = q;
                ct.SoLuong = i.SoLuong;
                pn.ChiTietPhieuNhap.Add(ct);

                var sach = (from s in context.SachList
                            where s.MaSach == i.MaSach
                            select s).FirstOrDefault();
                sach.SoLuong += i.SoLuong;
            }
            context.AddToPhieuNhapList(pn);
            context.SaveChanges();
            
        }
        public static void Insert(PhieuNhapDTO phieuNhap)
        {
            NhaSachEntities context = new NhaSachEntities();
            PhieuNhap pn = new PhieuNhap();
            pn.NgayNhap=phieuNhap.NgayNhap;
            pn.TinhTrang = phieuNhap.TinhTrang;

            
        }
        public static int GetCurrentID()
        {
            NhaSachEntities context = new NhaSachEntities();
            Nullable<int> id = context.GetCurrentID("PhieuNhap").SingleOrDefault();
            return (int) id;     
        }
        public static bool Exists(int maPhieuNhap)
        {
            NhaSachEntities context = new NhaSachEntities();

            var q = (from pn in context.PhieuNhapList
                     where pn.MaPhieuNhap == maPhieuNhap
                     select pn).SingleOrDefault();
            if (q == null)
                return false;
            return true;
        }
    }
}
